from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config

db = SQLAlchemy()


def create_app(config_name):
    # 创建 flask 实例
    app = Flask(__name__)
    # 从配置类中加载配置
    app.config.from_object(config_name)

    app.config['SECRET_KEY'] = 'MakkaPakka'  # 加密的密钥
    app.config['SESSION_USE_SIGNER'] = True  # 是否对发送到浏览器上session的cookie值进行加密
    app.config['SESSION_TYPE'] = 'redis'  # session类型为redis

    # 初始化用到的各个模块（关联到当前app）
    db.init_app(app)
    db.app = app

    # 注册蓝图
    from app.views.user import bp as user_blueprint
    from app.views.register import bp as register_blueprint
    from app.views.AllBooks import bp as AllBooks_blueprint
    from app.views.BorrowerInformation import bp as BorrowerInformation_blueprint
    from app.views.BooksOfLent import bp as BooksOfLent_blueprint
    from app.views.AdminInformation import bp as AdminInformation_blueprint
    from app.views.noticeOp import bp as Notice_blueprint
    from app.views.Diagram import bp as Diagram_blueprint
    app.register_blueprint(user_blueprint)
    app.register_blueprint(register_blueprint)
    app.register_blueprint(AllBooks_blueprint)
    app.register_blueprint(BorrowerInformation_blueprint)
    app.register_blueprint(BooksOfLent_blueprint)
    app.register_blueprint(AdminInformation_blueprint)
    app.register_blueprint(Notice_blueprint)
    app.register_blueprint(Diagram_blueprint)
    # 返回 flask 实例
    return app
